package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 创建Article对象, 包含属性: title,content
 * 程序启动后要求用户输入标题和正文, 然后将数据保存到article表中
 */
public class Test03 {
    public static void main(String[] args) {
        Article article = InputUtil.getInputObject(new Article(), "发布文章");
        try (Connection connection = DBUtil.getConnection()){
            /*
                INSERT INTO article(title,content) VALUES(?,?);
             */
            String sql = "INSERT INTO article(title,content) VALUES(?,?);";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,article.getTitle());
            ps.setString(2,article.getContent());
            int num = ps.executeUpdate();
            if (num>0)
                System.out.println("文章发布成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
